from typing import List

from database.mysqlhelper import MySqLHelper

db = MySqLHelper()

select_param_str = 'select f_code, f_monitor_date, f_monitor_name, f_monitor_type, f_monitor_result, f_update_time from t_monitor_result '
insert_param_str = 'insert into t_monitor_result (f_code, f_monitor_date, f_monitor_name, f_monitor_type, f_monitor_result) VALUES (%s, %s, %s, %s, %s) '
update_param_str = 'update t_monitor_result set '


class MonitorResult:
    def __init__(self, code, monitor_date, monitor_name, monitor_type: str, monitor_result, up_time):
        self.code = code
        self.monitor_date = monitor_date
        self.monitor_name = monitor_name
        self.monitor_type = monitor_type
        self.monitor_result = monitor_result
        self.up_time = up_time


# 用户监控
def select_with_date_type(m_date, m_type):
    # 增加多条
    sql = select_param_str + ' where f_monitor_date = %s and f_monitor_type =%s '
    return format_mul(db.selectall(sql, (m_date, m_type)))


# 插入多条
def insert_mul(data_list):
    d_len = len(data_list)
    if d_len == 0:
        print('t_monitor_result插入的数据为0')
        return 0
    # 增加多条
    li = []
    for info in data_list:
        li.append(
            (info['code'], info['monitor_date'], info['monitor_name'], info['monitor_type'], info['monitor_result']))
    return db.insertmany(insert_param_str, li)


# 插入一条数据
def insert(data):
    if not data:
        return 0
    li = (data['code'], data['monitor_date'], data['monitor_name'], data['monitor_type'], data['monitor_result'])
    return db.insertone(insert_param_str, li)


# 获取一条
def selectone(code, m_type):
    sql = select_param_str + ' where f_code = %s and f_monitor_type = %s order by f_id desc limit 1 '
    return format_one(db.selectone(sql, (code, m_type)))


# 获取多条
def select_size(code, m_type, size):
    sql = select_param_str + ' where f_code = %s and f_monitor_type = %s order by f_id desc limit %s '
    return format_res(db.selectall(sql, (code, m_type, size)))


# 查询所有
def select_all(code):
    # 增加多条
    sql = select_param_str + ' where f_code = %s order by f_monitor_date desc '
    return format_res(db.selectall(sql, (code)))


# 用户监控
def select_date_type(m_date, m_type):
    # 增加多条
    sql = select_param_str + ' where f_monitor_date = %s and f_monitor_type =%s '
    return format_res(db.selectall(sql, (m_date, m_type)))


# 获取一条
def select_code_type_new_one(code, m_type):
    sql = select_param_str + ' where f_code = %s and f_monitor_type =%s order by f_id desc limit 1 '
    return format_one(db.selectone(sql, (code, m_type)))


# code下所有触发
# 按照时间升序
def select_by_code_type(code, m_type):
    sql = select_param_str + ' where f_code = %s and f_monitor_type =%s order by f_id '
    return format_res(db.selectall(sql, (code, m_type)))


# 格式化结果
def format_one(info):
    if not info:
        return {}
    return {'code': str(info[0], encoding="utf-8"), 'monitor_date': info[1],
            'monitor_name': info[2], 'monitor_type': info[3], 'monitor_result': str(info[4], encoding="utf-8"),
            'up_time': info[5]}


# f_code, f_monitor_date, f_monitor_type, f_monitor_result, f_update_time
# 格式化结果
def format_res(res):
    r_list = []
    for info in res:
        r_list.append(format_one(info))
    return r_list


# 插入一条数据
def insert_one(mr: MonitorResult) -> int:
    if not mr:
        return 0
    return db.insertone(insert_param_str,
                        (mr.code, mr.monitor_date, mr.monitor_name, mr.monitor_type, mr.monitor_result))


# 更新没有则
def up_or_in_by_code_rq(mr: MonitorResult) -> int:
    if not mr:
        return 0
    if select_by_date_type(mr.code, mr.monitor_date, mr.monitor_type) is None:
        return insert_one(mr)
    else:
        return up_by_code_rq(mr)


def up_by_code_rq(mr: MonitorResult) -> int:
    sql = update_param_str + ' f_monitor_name = %s, f_monitor_result = %s ' \
                             'where f_code = %s and f_monitor_type = %s and f_monitor_date = %s '
    return db.update(sql, (mr.monitor_name, mr.monitor_result, mr.code, mr.monitor_type, mr.monitor_date))


# 获取类型下最新一条
def select_by_date_type(code, rq, lx) -> MonitorResult:
    # 增加多条
    sql = select_param_str + ' where f_code = %s and f_monitor_date = %s and f_monitor_type =%s order by f_id desc limit 1'
    return format_info(db.selectone(sql, (code, rq, lx)))


# 获取最新多条
def select_size_new(code, m_type: int, size) -> List[MonitorResult]:
    sql = select_param_str + ' where f_code = %s and f_monitor_type = %s order by f_id desc limit %s '
    return format_mul(db.selectall(sql, (code, m_type, size)))


# f_code, f_monitor_date, f_monitor_type, f_monitor_result, f_update_time
# 格式化结果
def format_mul(res) -> List[MonitorResult]:
    r_list = []
    for info in res:
        r_list.append(format_info(info))
    return r_list


# 格式化结果
def format_info(info) -> MonitorResult:
    if not info:
        return
    return MonitorResult(str(info[0], encoding="utf-8"), info[1],
                         str(info[2], encoding="utf-8"), info[3],
                         str(info[4], encoding="utf-8"), info[5])


if __name__ == '__main__':
    select_all('t1qznXH')
